﻿using CSharpDay12Nov10.Data.Model;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity.Migrations;

namespace CSharpDay12Nov10.Data
{
    public static class Seeder
    {
        public static void Seed(ApplicationDbContext db, bool roles = false, bool users = false, bool tasks = false)
        {
            if (roles) SeedRoles(db);
            if (users) SeedUsers(db);
            if (tasks) SeedTasks(db);
        }
        private static void SeedRoles(ApplicationDbContext db)
        {
            var store = new RoleStore<IdentityRole>(db);
            var manager = new RoleManager<IdentityRole>(store);

            if (!db.Roles.Any(x => x.Name == "User"))
            {
                manager.Create(new IdentityRole() { Name = "User" });
            }
            if (!db.Roles.Any(x => x.Name == "Admin"))
            {
                manager.Create(new IdentityRole() { Name = "Admin" });
            }
            db.SaveChanges();
        }
        private static void SeedUsers(ApplicationDbContext db)
        {
            var store = new UserStore<ApplicationUser>(db);
            var manager = new UserManager<ApplicationUser>(store);

            if (!db.Users.Any(x => x.UserName == "user1"))
            {
                ApplicationUser User = new ApplicationUser()
                {
                    UserName = "user1",
                    Email = "test@test.com"
                };
                manager.Create(User, "123123");
                manager.AddToRole(User.Id, "User");
            }
            if (!db.Users.Any(x => x.UserName == "user2"))
            {
                ApplicationUser User = new ApplicationUser()
                {
                    UserName = "user2",
                    Email = "testaccount@test.com"
                };
                manager.Create(User, "123123");
                manager.AddToRole(User.Id, "User");
            }
            if (!db.Users.Any(x => x.UserName == "admin"))
            {
                ApplicationUser User = new ApplicationUser()
                {
                    UserName = "admin",
                    Email = "admin@test.com"
                };
                manager.Create(User, "123123");
                manager.AddToRole(User.Id, "Admin");
            }
            db.SaveChanges();
        }
        private static void SeedTasks(ApplicationDbContext db)
        {
            string AdminId = db.Users.Where(x => x.UserName == "admin").Select(x => x.Id).FirstOrDefault();
            string User1Id = db.Users.Where(x => x.UserName == "user1").Select(x => x.Id).FirstOrDefault();
            string User2Id = db.Users.Where(x => x.UserName == "user2").Select(x => x.Id).FirstOrDefault();


            db.ToDos.AddOrUpdate(x => x.TaskToDo,
                new ToDo() { TaskToDo = "Teach Authentication", UserId = AdminId, DateCreated = DateTime.Now, IsComplete = false, IsDeleted = false }, 
                new ToDo() { TaskToDo = "Wake Up", UserId = AdminId, DateCreated = DateTime.Now, IsComplete = true, IsDeleted = false, DateCompleted = DateTime.Now }, 
                new ToDo() { TaskToDo = "Eat", UserId = AdminId, DateCreated = DateTime.Now, IsComplete = false, IsDeleted = true, DateDeleted = DateTime.Now },
                new ToDo() { TaskToDo = "Go For a Run", UserId = AdminId, DateCreated = DateTime.Now, IsComplete = true, IsDeleted = false, DateCompleted = DateTime.Now }, 
                new ToDo() { TaskToDo = "Walk The Dog", UserId = User1Id, DateCreated = DateTime.Now, IsComplete = false, IsDeleted = false },
                new ToDo() { TaskToDo = "Call Dad", UserId = User1Id, DateCreated = DateTime.Now, IsComplete = true, IsDeleted = true, DateCompleted = DateTime.Now, DateDeleted = DateTime.Now },
                new ToDo() { TaskToDo = "Pretend to Call Dad", UserId = User1Id, DateCreated = DateTime.Now, IsComplete = false, IsDeleted = false },
                new ToDo() { TaskToDo = "Talk To Recruiters", UserId = User1Id, DateCreated = DateTime.Now, IsComplete = true, IsDeleted = false, DateCompleted = DateTime.Now },
                new ToDo() { TaskToDo = "Play Video Games", UserId = User2Id, DateCreated = DateTime.Now, IsComplete = false, IsDeleted = true, DateDeleted = DateTime.Now },
                new ToDo() { TaskToDo = "Use Youtube", UserId = User2Id, DateCreated = DateTime.Now, IsComplete = true, IsDeleted = false, DateCompleted = DateTime.Now },
                new ToDo() { TaskToDo = "Chat On Facebook", UserId = User2Id, DateCreated = DateTime.Now, IsComplete = false, IsDeleted = false },
                new ToDo() { TaskToDo = "Text People", UserId = User2Id, DateCreated = DateTime.Now, IsComplete = true, IsDeleted = true, DateCompleted = DateTime.Now, DateDeleted = DateTime.Now }
                );
            db.SaveChanges();
        }
    }
}
